package com.drg.center.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.drg.center.entity.enums.UserAccTypeEnum;
import com.drg.center.entity.enums.UserStatusEnum;
import com.drg.center.entity.param.SysUserListDTO;
import com.drg.center.mapper.SysUserMapper;
import com.guaike.core.mybatis.plugins.BaseDAO;
import com.drg.center.entity.SysUserPO;
import jodd.util.StringUtil;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Set;

/**
 * @Author: zhangxilin
 * @Date: 2024/5/14
 */
@Repository
public class SysUserDAO extends BaseDAO<SysUserMapper, SysUserPO> {

    public IPage<SysUserPO> findPage(SysUserListDTO param, Set<Long> userIdSet){
        LambdaQueryWrapper<SysUserPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserPO::getAccType, UserAccTypeEnum.GENERAL_USER.getType());
        lambdaQueryWrapper.in(StringUtil.isNotBlank(param.getDeptCode()) && !CollectionUtils.isEmpty(userIdSet), SysUserPO::getId, userIdSet);
        lambdaQueryWrapper.eq(param.getStatus() != null, SysUserPO::getStatus, param.getStatus());
        lambdaQueryWrapper.like(StringUtil.isNotBlank(param.getRealName()),SysUserPO::getRealName,param.getRealName());
        lambdaQueryWrapper.orderByDesc(SysUserPO::getCreateTime);
        return getBaseMapper().selectPage(new Page<>(param.getPageIndex(), param.getPageSize()), lambdaQueryWrapper);
    }

    public SysUserPO findByUsernameAndNotDisable(String username){
        LambdaQueryWrapper<SysUserPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserPO::getUsername, username);
        lambdaQueryWrapper.ne(SysUserPO::getStatus, UserStatusEnum.DISABLE.getCode());
        return getBaseMapper().selectOne(lambdaQueryWrapper);
    }


}
